package com.deng.performance.dao.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.deng.common.SQLHelper;
import com.deng.common.vo.DispenseOrganizationVo;
import com.deng.common.vo.EmployeeVo;
import com.deng.common.vo.OperateDepartmentVo;
import com.deng.performance.dao.IOperateEmployee;
import com.deng.util.GlobalProperties;

public class OperateEmployeeImpl extends SQLHelper implements IOperateEmployee {

	public void updateByNewFunction(OperateDepartmentVo vo) {
		try{
			Connection conn = getConnect();
			String sql = "select * from t_sys_stat_operate_department where id = "+vo.id;
			ResultSet rs = queryData(conn,sql);
			if(rs.next()){
				String formula = rs.getString("formula");
				if(formula == null){
					return;//没定义公式
				}
					BigDecimal firstValue = new BigDecimal(vo.cal_value);//一次分配奖金总额
					sql = "select sum(jingyingjixiaogangweixishu) as x from t_stat_employee_month"+
							"	where jingyingjixiaohesuandanyuan = " +vo.id+
							"	and stat_time ='"+vo.stat_time+"'";
					ResultSet rs3 = queryData(conn, sql);
					BigDecimal totalPoint = new BigDecimal(0);
					if(rs3.next()){
						totalPoint = rs3.getBigDecimal(1);
					}else{
						System.out.println("没有算出本科室的总系数合");
						return;
					}
					
					sql = "select * from t_stat_employee_month " +
						"	where jingyingjixiaohesuandanyuan = " +vo.id+
						"	and stat_time ='"+vo.stat_time+"'";
					PreparedStatement pst = 
						conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
					ResultSet rs4 = pst.executeQuery();
					while(rs4.next()){
						int valid = rs4.getInt("valid");
						BigDecimal jingyingjixiaogangweixishu = rs4.getBigDecimal("jingyingjixiaogangweixishu");
						BigDecimal empBonus = jingyingjixiaogangweixishu.divide(totalPoint,3,BigDecimal.ROUND_HALF_UP).multiply(firstValue);
						if(valid == 0){//未审核
							rs4.updateBigDecimal("pay_jingyingjixiao", empBonus);
							rs4.updateBigDecimal("cal_jingyingjixiao", empBonus);
						}else{
							rs4.updateBigDecimal("cal_jingyingjixiao", empBonus);
						}		
						rs4.updateRow(); 
					}					
//				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public void updateByOldFunction(OperateDepartmentVo vo) {
		// TODO Auto-generated method stub

	}

	public List<EmployeeVo> updateAndGetByNewFunction(OperateDepartmentVo vo) {
		try{
			updateByNewFunction(vo);
			String sql = "select * from t_stat_employee_month " +
				"	where jingyingjixiaohesuandanyuan = " +vo.id+
				"	and stat_time ='"+vo.stat_time+"'";
			Connection conn = getConnect();
			ResultSet rs2 = queryData(conn, sql);
			EmployeeVo evo;
			List<EmployeeVo> l = new ArrayList<EmployeeVo>();
			while(rs2.next()){
				int employee_id = rs2.getInt("employee_id");
				evo = new EmployeeVo();
				evo.id = employee_id;
				evo.valid = rs2.getInt("valid");
				evo.name = rs2.getString("name");
				evo.work_id = rs2.getString("work_id");
				evo.stat_time = vo.stat_time;
				evo.pay_gongzuojixiao = rs2.getBigDecimal("pay_gongzuojixiao")!=null?rs2.getBigDecimal("pay_gongzuojixiao").toString():"0";
				evo.cal_gongzuojixiao = rs2.getBigDecimal("cal_gongzuojixiao")!=null?rs2.getBigDecimal("cal_gongzuojixiao").toString():"0";
				evo.pay_jingyingjixiao = rs2.getBigDecimal("pay_jingyingjixiao")!=null?rs2.getBigDecimal("pay_jingyingjixiao").toString():"0";
				evo.cal_jingyingjixiao = rs2.getBigDecimal("cal_jingyingjixiao")!=null?rs2.getBigDecimal("cal_jingyingjixiao").toString():"0";
				l.add(evo);
			}
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

}
